﻿using System;
using System.Windows;
using System.Data;
namespace WPFExcelReport
{
    public partial class UIFOLDER : ViewBase
    {
        public VICC_FOLDERS TMV
        {
            get
            {
                return (VICC_FOLDERS)MV;
            }
        }
        #region ==PHOTOLINK===========
        public static readonly DependencyProperty PHOTOLINKProperty = DependencyProperty.Register("PHOTOLINK",
        typeof(string), typeof(UIFOLDER),
        new FrameworkPropertyMetadata("/ImagePackage;component/Images/Chart.png", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPHOTOLINKChanged));
        public string PHOTOLINK
        {
            get
            {
                return (string)GetValue(PHOTOLINKProperty);
            }
            set
            {
                SetValue(PHOTOLINKProperty, value);
            }
        }
        private static void OnPHOTOLINKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIFOLDER;
            if (src != null)
            {
                src.PHOTOLINK = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsImageMax===========
        public static readonly DependencyProperty IsImageMaxProperty = DependencyProperty.Register("IsImageMax",
        typeof(bool), typeof(UIFOLDER),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsImageMaxChanged));
        public bool IsImageMax
        {
            get
            {
                return (bool)GetValue(IsImageMaxProperty);
            }
            set
            {
                SetValue(IsImageMaxProperty, value);
            }
        }
        private static void OnIsImageMaxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIFOLDER;
            if (src != null)
            {
                src.IsImageMax = (bool)e.NewValue;
            }
        }
        #endregion
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
            }
            else
            {
                KEY = TMV.COMPANY.Trim().PadRight(10) + TMV.ROOT.Trim().PadRight(200) + TMV.SUB.PadRight(40);
            }
        }
        public override void OnEXAUFChg(string value)
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (value == "")
                return;
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["LINKER"] = EXAUF;
            SResult = TMV.QFILELINKER(dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string version = row["VERSION"].ToString().Trim();
                string root = row["ROOT"].ToString().Trim();
                string sub = row["SUB"].ToString().Trim();
                byte[] content = (byte[])row["FBCONTENT"];
                PHOTOLINK = root + "\\" + sub;
                if (!System.IO.File.Exists(PHOTOLINK))
                {
                    Helpers.Helper.byte2file(root + "\\" + sub, content);
                }
            }

            base.OnEXAUFChg(value);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            dwMaster["LINKER"] = EXAUF;
            TMV.UFOLDER03(dwMaster);
            base.OnSave();
        }
    }
}
